<?php
namespace Models;
use Models\Photo;

/**
 *
 * @author Eugene Baranov <evgeny@baranov.eu>
 *
 */
class Album extends Base {
    protected $object_id;
    protected $owner;
    protected $name;
    protected $link;
    protected $photo_count;

    /**
     * @var Photo
     */
    protected $cover;
    protected $cover_object_id;
    protected $description;

    /**
     * @var Photo[]
     */
    protected $photos;

    public function toArray() {
        return array_merge(parent::toArray(), array(
            'owner' => $this->owner,
            'name' => $this->name,
            'object_id' => $this->object_id,
            'link' => $this->link,
            'photo_count' => $this->photo_count,
            'updated' => $this->updated,
            'created' => $this->created,
            'description' => $this->description,
            'cover_object_id' => $this->cover_object_id
        ));
    }

    public function getId() {
        return $this->object_id;
    }

    public function getName() {
        return $this->name;
    }

    public function getObjectId() {
        return $this->object_id;
    }

    public function getLink() {
        return $this->link;
    }

    /**
     * @return number
     */
    public function getPhotoCount() {
        return intval($this->photo_count);
    }

    /**
     * @return boolean
     */
    public function havePhotos() {
        return $this->getPhotoCount() > 0;
    }

    /**
     * @return Photo[]
     */
    public function getPhotos() {
        return $this->photos;
    }

    /**
     * @return Photo
     */
    public function getCover() {
        return $this->cover;
    }

    /**
     * @param array $data
     * @return Base
     */
    public function fromArray($data) {
        if (!empty($data['cover_object_id']) && is_array($data['photos'])) {
            foreach ($data['photos'] as $p) {
                if ($p->getId() == $data['cover_object_id']) {
                    $data['cover'] = $p;
                }
            }
        } else {
            $data['cover'] = $data['photos'][0];
        }
        parent::fromArray($data);
    }

    /**
     *
     * @param number $n
     * @return Photo
     */
    public function getPhotoAt($n) {
        return $this->photos[$n];
    }
}